package com.itheima.service.impl.permission;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.permission.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.permission.PermissionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 陈功炳
 * @since 2021/11/5 16:02
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    /**
     *分页查询
     * @param queryPageBean 前端的page size和查询条件
     * @return 返回总记录数和当前页结果
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<Permission> byPage = permissionDao.findByPage(queryString);
        long total = byPage.getTotal();
        List<Permission> result = byPage.getResult();

        return new PageResult(total,result);
    }

    /**
     * 添加功能
     * @param permission 前端输入的数据
     * @return 返回boolean给前端校验
     */
    @Override
    public Boolean add(Permission permission) {
        Integer add = permissionDao.add(permission);
        return add > 0;
    }

    /**
     * 根据id查询
     * @param id 传来要查询的id
     * @return 返回boolean给前端校验
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     * 修改功能
     * @param permission 前端传来的修改数据
     * @return 返回boolean传给前端校验
     */
    @Override
    public Boolean edit(Permission permission) {
        Integer edit = permissionDao.edit(permission);
        return edit > 0;
    }

    /**
     * 删除功能
     * @param id 前端传的要删除数据的id
     * @return 返回boolean传给前端校验
     */
    @Override
    public Boolean delete(Integer id) {
        permissionDao.deleteT(id);
        Integer delete = permissionDao.delete(id);
        return delete > 0;
    }
}
